pro slugplot, ind
common slug, status, seds, wave

rplot, wave, seds[ind].sed, ylog=status.ylog, xlog=status.xlog,$
	/xsty, /ysty, xr=status.lrange, yr=status.frange
widget_control, status.next, sensiti=n_elements(seds)-1 GT ind
widget_control, status.prev, sensiti= ind GT 0
end
;-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
pro specslug_event, ev
common slug, status, seds, wave
widget_control, ev.id, get_uvalue=uvalue

case uvalue of
'save':begin
	mwrfits, seds, status.outfile, /create
	end
'savename':begin
	res=rds_fileselect(base=status.tlb, /checkclobber)
	if strmatch(res, '') EQ 0 then status.outfile=res
	end
;'set directory':begin
;	   res=rds_fileselect(base=status.tlb)
;	   if strmatch(res, '*/') EQ 0 then begin
;	   split1=strsplit(res, '/')		
;	   status.dir=strmid(res, 0, split1[n_elements(split1)-1])
;	   endif else status.dir=res
;	end
'imf': status.imf_ind=ev.index
'metal': status.met_ind=ev.index
'age':begin
	widget_control, ev.id, get_value=val1
	status.age=val1
	end
'howmany':begin
	widget_control, ev.id, get_value=val1
        status.howmany=round(float(val1))
	widget_control,ev.id, set_value=rstring(status.howmany)
	end
'nstars':begin
	widget_control, ev.id, get_value=val1
        status.nstars=round(float(val1))
	widget_control,ev.id, set_value=rstring(status.nstars)
	end
'make models': begin
	stochastic_sed,status.imfls[status.imf_ind],$
		 status.metls2[status.met_ind], status.age,  $
		 status.howmany, status.nstars, seds=seds
	slugplot, 0
	end
'lammin':begin
	widget_control, ev.id, get_value=val1
        status.lrange[0]=val1
	end
'lammax':begin
        widget_control, ev.id, get_value=val1
        status.lrange[1]=val1
        end
'fmin':begin
        widget_control, ev.id, get_value=val1
        status.frange[0]=val1
        end
'fmax':begin
        widget_control, ev.id, get_value=val1
        status.frange[1]=val1
        end
'log wave':status.xlog*=-1
'log flux':status.ylog*=-1
'previous': slugplot, ind+1
'next': slugplot, ind-1
else:
endcase

if strmatch(uvalue, 'quit') then widget_control, ev.top, /destroy
end
;-*-*-*-*-*-*-*-*-*-*-*-*-*-*
pro spec_slug
common slug, status, seds, wave
;ls=[0.004,0.001,0.004,0.008,0.020,0.04,0.1]
ls='solar'
;metls2=['0004','001','004','008','020','040','100']
status={tlb:0L, dir:'$SLUG_DIR/data/', draw:0L, imfbox:0L, metalbox:0L, $
	howmanybox:0L, howmany:1L, lamminb:0L, lammaxb:0L,$
	fminb:0L, fmaxb:0L, metls:ls, imfls:['Salpeter'],$
	imf_ind:0L, met_ind:0L, age:0., lrange:fltarr(2), frange:fltarr(2), outfile:'',$
	xlog:-1, ylog:-1, ind:0L, n_ind:0L, $
	metls2:['020'], prev:0L, next:0L,$
	nstars:100L}

wave=mrdfits('lej_p00_lambda_nm.fits')*10

status.tlb=widget_base( xsize=650, ysize=600, /col, title='Spec Slug')
row1=widget_base(status.tlb ,/row)
quit=widget_button(row1, uvalue='quit', value='Quit')
sav=widget_button(row1, uvalue='save', value='Save')
sav=widget_button(row1, uvalue='savename', value='Set Save File')
;set_dir=widget_button(row1, uvalue='set dir', value='Set Model Directory')
row2=widget_base(status.tlb, /row)
lbl=widget_label(row2, value='IMF Type:')
status.imfbox=widget_droplist(row2, value=status.imfls, uvalue='imf')
lbl=widget_label(row2, value='Metallicity:')
status.metalbox=widget_droplist(row2, value=status.metls, uvalue='metal')
lbl=widget_label(row2, value='Age')
status.howmanybox=widget_text(row2, xsize=6, value='0.0', /editable, /all, uvalue='age')
lbl=widget_label(row2, value='How Many SEDs?')
status.howmanybox=widget_text(row2, xsize=6, value='1', /editable, /all, uvalue='howmany')
row25=widget_base(status.tlb, /row)
lbl=widget_label(row25, value='Number of Stars')
box=widget_text(row25, xsize=6, value='100', /editable, /all, uvalue='nstars')

btn=widget_button(row25, value='Make Models', uvalue='make models')
row3=widget_base(status.tlb,/row)
lbl=widget_label(row3, value='Wavelength Range [Ang]')
status.lamminb=widget_text(row3, xsize=6, value='', /editable, /all, uvalue='lammin')
status.lammaxb=widget_text(row3, xsize=6, value='', /editable, /all, uvalue='lammax')
lbl=widget_label(row3, value='Flux range')
status.fminb=widget_text(row3, xsize=6, value='', /editable, /all, uvalue='fmin')
status.fmaxb=widget_text(row3, xsize=6, value='', /editable, /all, uvalue='fmax')
nonex=widget_base(row3, /row, /nonexclusive)
btn=widget_button(nonex, value='log wave', uvalue='log wave')
btn=widget_button(nonex, value='log flux', uvalue='log flux')


geo=widget_info(status.tlb, /geo)
status.draw=widget_draw(status.tlb, xsize=geo.xsize-10, ysize=400, xoffset=5)
bot_row=widget_base(status.tlb, /row, /align_center)
status.prev=widget_button(bot_row, value='<< Previous', uvalue='previous',sensit=0)
status.next=widget_button(bot_row, value='Next >>', uvalue='next', sensit=0)



widget_control, status.tlb, /realize
xmanager, 'specslug', status.tlb, /no_block
end
